using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using LocalizationTest.Tools;

namespace LocalizationTest
{
  public class MapWithDoors : Map
  {
    private struct Door
    {
      public double center;
      public double width;
//      public double center2;
//      public double width2;

      public Door(double center, double width, bool[] doorsAsArray, double rasterWidth)
      {
        int centerSegmentIndex = Globals.PositionToIndex(center, rasterWidth, Globals.stateSpaceMinimumX);
        int widthInSegments = (int)Math.Round(width / rasterWidth);
        if (widthInSegments == 0) widthInSegments = 1;

        int leftDoorSegment = centerSegmentIndex - widthInSegments / 2;

        if (leftDoorSegment < 0) leftDoorSegment = 0;

        for (int i = 0; i < widthInSegments; i++)
        {
          if (leftDoorSegment + i < doorsAsArray.Length) 
            doorsAsArray[leftDoorSegment + i] = true;
        }
        this.center = center;
        this.width = width;
        /* 
         double newDoorPosition = Globals.stateSpaceMinimumX + centerSegmentIndex * rasterWidth;
         if (widthInSegments % 2 == 0) newDoorPosition -= rasterWidth / 2.0;
         double newDoorWidth = widthInSegments * rasterWidth;



         this.center2 = newDoorPosition;
         this.width2 = newDoorWidth;
         */ 
      }
    };

    double rasterWidth = 25;

    public double RasterWidth
    {
      get { return rasterWidth; }
    }

    List<Door> doorsPositionAndWidth;
    bool[] doorsAsArray;

    public bool[] DoorsAsArray
    {
      get { return doorsAsArray; }
    }
    int numberOfDoorSegments;

    public MapWithDoors()
    {
      int sizeOfStateSpace = (int)Math.Round((double)(Globals.stateSpaceMaximumX - Globals.stateSpaceMinimumX) / rasterWidth);
      doorsAsArray = new bool[sizeOfStateSpace+1];

      doorsPositionAndWidth = new List<Door>();

//      doorsPositionAndWidth.Add(new Door(-400, 50, doorsAsArray, rasterWidth));
      doorsPositionAndWidth.Add(new Door(-300, 50, doorsAsArray, rasterWidth));
//      doorsPositionAndWidth.Add(new Door(-200, 50, doorsAsArray, rasterWidth));
//      doorsPositionAndWidth.Add(new Door(-100, 50, doorsAsArray, rasterWidth));
//      doorsPositionAndWidth.Add(new Door(-000, 50, doorsAsArray, rasterWidth));
//      doorsPositionAndWidth.Add(new Door(+100, 50, doorsAsArray, rasterWidth));
      doorsPositionAndWidth.Add(new Door(+200, 50, doorsAsArray, rasterWidth));
//      doorsPositionAndWidth.Add(new Door(+300, 50, doorsAsArray, rasterWidth));
//      doorsPositionAndWidth.Add(new Door(+400, 50, doorsAsArray, rasterWidth));
//      doorsPositionAndWidth.Add(new Door(+500, 50, doorsAsArray, rasterWidth));
//      doorsPositionAndWidth.Add(new Door(+1000, 50, doorsAsArray, rasterWidth));

      numberOfDoorSegments = 0;
      for (int i = 0; i < doorsAsArray.Length; i++)
      {
        if (doorsAsArray[i]) numberOfDoorSegments++;
      }
    }

    public override void Draw()
    {
      //coordinate system
      DebugDrawingTool.CreateLine("world:_map", new Vector2(-800, 0), new Vector2(800, 0), 3, Color.Gray);
      DebugDrawingTool.CreateLine("world:_map", new Vector2(0, -800), new Vector2(0, 800), 3, Color.Gray);

      /*
      foreach (Door door in doorsPositionAndWidth)
      {
        DebugDrawingTool.CreateLine("world:_map doors", 
          new Vector2(door.center, -10),
          new Vector2(door.center, 20),
          door.width,
          Color.Red);
      }*/


      Pen orangePen = new Pen(Color.Orange, 90);
      for (int i = 0; i < doorsAsArray.Length; i++)
      {
        if (doorsAsArray[i])
        {
          DebugDrawingTool.CreateLine("world:_map",
            new Vector2((Globals.stateSpaceMinimumX + i * rasterWidth - rasterWidth / 2.0), -70),
            new Vector2((Globals.stateSpaceMinimumX + i * rasterWidth + rasterWidth / 2.0 + 0.5), -70), 90, Color.Orange);
        }
        else
        {
          /*
          DebugDrawingTool.CreateLine("world:_map",
            new Vector2((Globals.stateSpaceMinimumX + i * rasterWidth - rasterWidth / 3.0), -60),
            new Vector2((Globals.stateSpaceMinimumX + i * rasterWidth + rasterWidth / 3.0 + 0.5), -60), 70, Color.Green);
           */ 
        }

      }
    }
  }
}
